package atom.core3.hibernate.oracle;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Proxy;
import java.sql.Blob;

import oracle.sql.BLOB;

import org.apache.log4j.Logger;
import org.hibernate.LobHelper;
import org.hibernate.SessionFactory;
import org.hibernate.engine.jdbc.SerializableBlobProxy;

import atom.core0.exception.BizSysException;


public class OracleBlobUtil
{
	private Logger logger = Logger.getLogger(OracleBlobUtil.class);

	public static Blob fileToBlob(SessionFactory sessionFactory,String filename) throws BizSysException
	{
		Blob blob = null;
		try
		{
			FileInputStream fis = new FileInputStream(filename);
			LobHelper lobHelper = sessionFactory.getCurrentSession().getLobHelper();
			blob = lobHelper.createBlob(fis,fis.available());
		}
		catch (Exception e)
		{
			throw new BizSysException("从文件[" + filename + "]获取Blob对象失败");
		}
		return blob;

	}
	//代码可以直接使用Hibernate.createBlob(is);
	public static Blob streamToBlob(SessionFactory sessionFactory,InputStream is) throws BizSysException
	{
		Blob blob = null;
		try
		{
			LobHelper lobHelper = sessionFactory.getCurrentSession().getLobHelper();
			blob = lobHelper.createBlob(is,is.available());;
		}
		catch (Exception e)
		{
			throw new BizSysException("从流获取Blob对象失败");
		}
		return blob;

	}
	
	public static void updateByteToOracleBlob(byte[] data,Blob blob) throws BizSysException
	{
		if (blob==null)
			throw new BizSysException("Blob参数是空");
		try
		{
			SerializableBlobProxy  proxy = (SerializableBlobProxy)Proxy.getInvocationHandler(blob);
			proxy.getWrappedBlob();
			Blob newBlob = proxy.getWrappedBlob();
			
			OutputStream out = null;
			if (newBlob instanceof BLOB)
			{
				out = ((BLOB)newBlob).getBinaryOutputStream();
				out.write(data);
				out.flush();
				out.close();
			}
		}
		catch (Exception e)
		{
			throw new BizSysException("从流获取Blob对象失败");
		}

	}
	
	public static void updateStreamToOracleBlob(InputStream fis,Blob blob) throws BizSysException
	{
		if (blob==null)
			throw new BizSysException("Blob参数是空");
		try
		{
			SerializableBlobProxy  proxy = (SerializableBlobProxy)Proxy.getInvocationHandler(blob);
			proxy.getWrappedBlob();
			Blob newBlob = proxy.getWrappedBlob();
			
			byte[] data = new byte[(int)fis.available()];
			fis.read(data);
			OutputStream out = null;
			if (newBlob instanceof BLOB)
			{
				out = ((BLOB)newBlob).getBinaryOutputStream();
				out.write(data);
				out.flush();
				out.close();
			}
			fis.close();
		}
		catch (Exception e)
		{
			throw new BizSysException("从流获取Blob对象失败");
		}

	}
	
	public static void updateFileToOracleBlob(String filename,Blob blob) throws BizSysException
	{
		try
		{
			FileInputStream fis = new FileInputStream(filename);
			updateStreamToOracleBlob(fis,blob);
		}
		catch (Exception e)
		{
			throw new BizSysException("从文件[" + filename + "]获取Blob对象失败");
		}

	}
	//这两种方法都可与用
	public static Blob createEmptyBlob(SessionFactory sessionFactory) 
	{
//		Blob blob = null;
//		try
//		{
//			blob = oracle.sql.BLOB.empty_lob();
//		}
//		catch(Exception e)
//		{
//			e.printStackTrace();
//		}
//		return blob;
		Blob blob = null;
		LobHelper lobHelper = sessionFactory.getCurrentSession().getLobHelper();
		
		blob = lobHelper.createBlob(new byte[1]);
		return blob;		
	}
	//这两种方法都可与用
	public static BLOB createEmptyOracleBlob() throws Exception
	{
		return oracle.sql.BLOB.empty_lob();

	}
	//Hibernate.createBlob在hibernate3.5以后废弃了
	public static Blob createEmptyBlob_35(SessionFactory sessionFactory) 
	{
		Blob blob = null;
		LobHelper lobHelper = sessionFactory.getCurrentSession().getLobHelper();
		blob = lobHelper.createBlob(new byte[1]);
		return blob;

	}
	
}
